package com.example.demo.config;

import com.example.demo.entity.BatchPeople;
import com.example.demo.tool.TbUserTools;
import lombok.AllArgsConstructor;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.PromptChatMemoryAdvisor;
import org.springframework.ai.chat.client.advisor.SimpleLoggerAdvisor;
import org.springframework.ai.chat.client.advisor.vectorstore.QuestionAnswerAdvisor;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.chat.memory.ChatMemoryRepository;
import org.springframework.ai.chat.memory.InMemoryChatMemoryRepository;
import org.springframework.ai.chat.memory.MessageWindowChatMemory;
import org.springframework.ai.tool.ToolCallbackProvider;
import org.springframework.ai.vectorstore.VectorStore;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @ClassName BeanConfig
 * @Description:
 * @Author: hgq
 * @CreateDate: 2025/2/24 14:56
 * @UpdateUser: hgq
 * @UpdateDate: 2025/2/24 14:56
 * @UpdateRemark:
 * @Version: 1.0
 */
@Configuration
@AllArgsConstructor
public class BeanConfig {

    private ChatClient.Builder chatClientBuilder;
    private VectorStore theVectorStore;
    private TbUserTools tbUserTools;
    private ToolCallbackProvider toolCallbackProvider;

    @Bean
    public BatchPeople batchPeople() {
        BatchPeople batchPeople = BatchPeople
                .builder()
                .batchNo("20250224")
                .peopleId("1234")
                .vName("v222name")
                .build();

        return batchPeople;
    }

    @Bean
    public ChatClient chatClient () {
        return chatClientBuilder
                .defaultSystem("""
                        你是我的个人助手,需要通过中文和我互动,现在时间是 {current_date}
                        """)
                .defaultAdvisors(
                        PromptChatMemoryAdvisor
                                .builder(chatMemory())
                                .build(),
                        SimpleLoggerAdvisor
                                .builder()
                                .requestToString(request -> "请求: " + request.prompt().getUserMessage())
                                .responseToString(response -> "响应: " + response.getResult())
                                .order(4)
                                .build(),
                        QuestionAnswerAdvisor
                                .builder(theVectorStore)
                                .build())
                .defaultTools(tbUserTools)
                .defaultToolCallbacks(toolCallbackProvider)
                .build();
    }

    /**
     * 对话记忆
     * @return
     */
    @Bean
    public ChatMemory chatMemory(){
        // return new InMemoryChatMemoryRepository();
        ChatMemoryRepository repository = new InMemoryChatMemoryRepository();
        ChatMemory chatMemory = MessageWindowChatMemory.builder()
                .chatMemoryRepository(repository)
                .maxMessages(10)
                .build();
        return chatMemory;
    }
}
